package com.keeson.android_cli.util;


import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class DataConverter {
    private static DataConverter mDataConverter;
    private DataConverter() {
    }

    public static DataConverter getInstance() {
        if (mDataConverter == null) {
            mDataConverter = new DataConverter();
        }
        return mDataConverter;
    }


    public int toInt(byte[] array, int offset, int length) {
        int value = 0;
        for (int i = 0; i < length; i++) {
            int shift = (length - 1 - i) * 8;
            value += (array[i + offset] & 0x000000FF) << shift;
        }
        return value;
    }

    public int toIntByLittleEndian(byte[] array, int offset, int length) {
        int value = 0;
        for (int i = length - 1; i >= 0; i--) {
            int shift = (length - 1 - i) * 8;
            value += (array[offset + length - i - 1] & 0x000000FF) << shift;
        }
        return value;
    }

    public int toIntByLittleEndian(byte[] array, int offset) {
        return toIntByLittleEndian(array, offset, 4);
    }

    public short toShort(byte[] array, int offset, int length) {
        short value = 0;
        for (int i = 0; i < length; i++) {
            short shift = (short) ((length - 1 - i) * 8);
            value += (array[i + offset] & 0x000000FF) << shift;
        }
        return value;
    }

    public short toShortByLittleEndian(byte[] array, int offset, int length) {
        short value = 0;
        for (int i = length - 1; i >= 0; i--) {
            short shift = (short) ((length - 1 - i) * 8);
            value += (array[offset + length - i - 1] & 0x000000FF) << shift;
        }
        return value;
    }

    public short toShortByLittleEndian(byte[] array, int offset) {
        return toShortByLittleEndian(array, offset, 2);
    }

    public long toLong(byte[] array, int offset, int length) {
        long value = 0;
        for (int i = 0; i < length; i++) {
            long shift = (length - 1 - i) * 8;
            value += (Long.valueOf(array[i + offset]) & 0x00000000000000FF) << shift;
        }
        return value;
    }

    public long toLongByLittleEndian(byte[] array, int offset, int length) {
        long value = 0;
        for (int i = length - 1; i >= 0; i--) {
            long shift = (length - 1 - i) * 8;
            value += (array[offset + length - i - 1] & 0x00000000000000FFL) << shift;
        }
        return value;
    }

    public long toLongByLittleEndian(byte[] array, int offset) {
        return toLongByLittleEndian(array, offset, 8);
    }

    public String toString(byte[] array, int offset, int length) {
        return new String(array, offset, length);
    }

    public byte[] fromIntToLittleEndian(int v) {
        byte[] b = new byte[4];
        b[0] = (byte) (0xff & v);
        b[1] = (byte) ((0xff00 & v) >> 8);
        b[2] = (byte) ((0xff0000 & v) >> 16);
        b[3] = (byte) ((0xff000000 & v) >> 24);
        return b;
    }

    public byte[] fromInt(int v) {
        byte[] b = new byte[4];
        b[3] = (byte) (0xff & v);
        b[2] = (byte) ((0xff00 & v) >> 8);
        b[1] = (byte) ((0xff0000 & v) >> 16);
        b[0] = (byte) ((0xff000000 & v) >> 24);
        return b;
    }

    public byte[] fromShortToLittleEndian(short v) {
        byte[] b = new byte[2];
        b[0] = (byte) (0xff & v);
        b[1] = (byte) ((0xff00 & v) >> 8);
        return b;
    }

    public byte[] fromShort(short v) {
        byte[] b = new byte[2];
        b[1] = (byte) (0xff & v);
        b[0] = (byte) ((0xff00 & v) >> 8);
        return b;
    }

    public byte[] fromLongToLittleEndian(long v) {
        byte[] b = new byte[8];
        b[0] = (byte) (0XFFL & v);
        b[1] = (byte) ((0XFF00 & v) >> 8);
        b[2] = (byte) ((0XFF0000 & v) >> 16);
        b[3] = (byte) ((0XFF000000 & v) >> 24);
        b[4] = (byte) ((0XFF00000000L & v) >> 32);
        b[5] = (byte) ((0XFF0000000000L & v) >> 40);
        b[6] = (byte) ((0XFF000000000000L & v) >> 48);
        b[7] = (byte) ((0XFF00000000000000L & v) >> 56);
        return b;
    }

    public byte[] fromLong(long v) {
        byte[] b = new byte[8];
        b[7] = (byte) (0xffL & v);
        b[6] = (byte) ((0xff00L & v) >> 8l);
        b[5] = (byte) ((0xff0000L & v) >> 16l);
        b[4] = (byte) ((0xff000000L & v) >> 24l);
        b[3] = (byte) ((0xff00000000L & v) >> 32l);
        b[2] = (byte) ((0xff0000000000L & v) >> 40l);
        b[1] = (byte) ((0xff000000000000L & v) >> 48l);
        b[0] = (byte) ((0xff00000000000000L & v) >> 56l);
        return b;
    }

    public byte[] fromString(String str) {
        return str.getBytes();
    }

    public Date toDateFromBytes(byte[] t) {
        String year = String.valueOf(toInt(t[0]));
        String month = String.valueOf(toInt(t[1]));
        String day = String.valueOf(toInt(t[2]));
        String hour = String.valueOf(toInt(t[3]));
        String minute = String.valueOf(toInt(t[4]));
        String second = String.valueOf(toInt(t[5]));
        if (year.length() == 1)
            year = "0" + year;
        if (month.length() == 1)
            month = "0" + month;
        if (day.length() == 1)
            day = "0" + day;
        if (hour.length() == 1)
            hour = "0" + hour;
        if (minute.length() == 1)
            minute = "0" + minute;
        if (second.length() == 1)
            second = "0" + second;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        try {
            date = sdf.parse("20" + year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);
            return date;
        } catch (ParseException e) {
            return null;
        }

    }

    public byte[] toBytesFromDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ds = sdf.format(date);
        byte[] d = new byte[6];
        d[0] = (byte) Integer.parseInt(ds.substring(2, 4));
        d[1] = (byte) Integer.parseInt(ds.substring(5, 7));
        d[2] = (byte) Integer.parseInt(ds.substring(8, 10));
        d[3] = (byte) Integer.parseInt(ds.substring(11, 13));
        d[4] = (byte) Integer.parseInt(ds.substring(14, 16));
        d[5] = (byte) Integer.parseInt(ds.substring(17, 19));
        return d;

    }

    public int toInt(byte a) {
        return a & 0xff;
    }


    //从2000年1月1日0时0分0秒开始的秒数
    public  int  getCurrentTime() {
        int time = 0;//
        String startTime = "2000-01-01 00:00:00";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime()/1000;
        time = (int) (System.currentTimeMillis()/1000-ts);
        return time;
    }


    public String getTime(byte[] data){
        String result = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = "2000-01-01 00:00:00";
        Date date = null;
        try {
            date = simpleDateFormat.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime()/1000;
        int time = toInt(data,0,data.length);
        ts = (ts + time)*1000;
        date = new Date(ts);
        result = simpleDateFormat.format(date);
        Log.e("time====",result);

        return  result;

    }
}
